package com.cjm.nio.demo;


import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.ServerSocketChannel;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

public class Accept implements Runnable {
    private static ExecutorService mainReactor = Executors.newCachedThreadPool(new ThreadFactory() {
        private AtomicInteger num = new AtomicInteger(0);
        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread();
            t.setName("main-reactor-" + num.incrementAndGet());
            return t;
        }
    });



    @Override
    public void run() {
        ServerSocketChannel ssc = null;
        try {
            ssc = ServerSocketChannel.open();
            ssc.configureBlocking(false);
            ssc.bind(new InetSocketAddress(80));
            dispatch(ssc);
            System.err.println("服务成功启动。。。");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private void dispatch(ServerSocketChannel ssc) {
        mainReactor.submit(new MainReactor(ssc));
    }
}
